/**
 * @file common.h
 * @brief Defines common constructs that cannot be found in the z-stack headers.
 *        This file was created to bridge the differences between the z-stack
 *        header files and those of the Wizi header files.
 * @author Vamshi Ponnapalli
 */

#ifndef _COMMON_H
  #define _COMMON_H
  /*****************************************************************************
   * Includes
   ****************************************************************************/
  #include "hal_types.h"

  #ifdef __cplusplus
    extern "C" {
  #endif
  /*****************************************************************************
   * Types
   ****************************************************************************/   
  
  /*****************************************************************************
   * Constants
   ****************************************************************************/
  //Macros   
  #ifndef BM
    #define BM(n)      (1 << (n))
  #endif
  
      
  //Error codes
  #define ERROR_ETH_INIT_DHCP   0x81
  #define ERROR_ETH_TX_FAILED   0x82
  #define ERROR_ETH_RX_FAILED   0x83
      
  //Bit positions
  #define BIT0              0x01
  #define BIT1              0x02
  #define BIT2              0x04
  #define BIT3              0x08
  #define BIT4              0x10
  #define BIT5              0x20
  #define BIT6              0x40
  #define BIT7              0x80
  
  //Other constants
  #define NOP()  asm("NOP")
      
  #ifndef SUCCESS
    #define SUCCESS 0
  #else
    #ifdef __IAR_SYSTEMS_ICC__
      #warning "Macro SUCCESS already defined"
    #endif
  #endif
  
  #ifndef FAILED
    #ifndef WIN32
      #define FAILED  1
    #endif
  #else
    #ifdef __IAR_SYSTEMS_ICC__
      #warning "Macro FAILED already defined"
    #endif
  #endif
  
  /*****************************************************************************
   * Function declarations
   ****************************************************************************/
   /**
   * Reverses the bytes present in the buffer. This call can be used to change
   * the "endian-ness" of bytes or bytes within a word/dword.
   *
   * @param pBuffer A refernce to the buffer that needs its contents swapped.
   * @param size The size of the buffer in bytes.
   */
  void utils_reverseBuffer(uint8 *pBuffer, uint16 size);
  
  /**
   * Sleeps for the specified number of microseconds.
   * 
   * @param duration The number of microseconds to sleep for.
   */
  void utils_sleepUs(uint16 duration);
  
  /**
   * Generates a random 16 bit number. This is dependent on the random number 
   * used to seed the random number generator.
   * 
   * @return A randomly generated 16 bit number.
   */
  uint16 utils_getRandom(void);
  
  /**
   * Seeds the random number generator with a randomly generated number.
   */
  void utils_initializeRandom(void);
  
  #ifdef __cplusplus
    }
  #endif

#endif